import Cookies from 'js-cookie'
const TokenKey = 'X-Token'

export function getToken() {
    return Cookies.get(TokenKey)
}

export function setToken(token) {
    return Cookies.set(TokenKey, token)
}

export function removeToken() {
    return Cookies.remove(TokenKey)
}

export function getDeviceId() {
    return localStorage.getItem('Device-Id')
}

export function setDeviceId() {
    var deviceId = localStorage.getItem('Device-Id')
    window.localStorage.setItem('erp-d', 'Browser')
    if (deviceId !== null && deviceId !== undefined) {
        return deviceId
    }

    var len = 32
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length
    var pwd = ''
    for (var i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }

    var timestamp = new Date().getTime()
    var randomStr = String(Math.random() * timestamp).replace(/\./g, '')
    deviceId = pwd + randomStr
    localStorage.setItem('Device-Id', deviceId)
    //
    return deviceId
}
